package us.team.openAnAppAtRandom.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import us.team.openAnAppAtRandom.model.AppItem;
import us.team.openAnAppAtRandom.model.AppList;

/**
 * 应用清单管理工具类
 */
public class AppListManager {
    private static final String TAG = "AppListManager";
    private static final String PREF_NAME = "app_lists";
    private static final String KEY_LISTS = "lists";
    
    private Context context;
    private List<AppList> appLists;
    
    public AppListManager(Context context) {
        this.context = context;
        this.appLists = loadAppLists();
    }
    
    /**
     * 获取所有应用清单
     */
    public List<AppList> getAppLists() {
        return appLists;
    }
    
    /**
     * 添加新的应用清单
     */
    public void addAppList(AppList appList) {
        appLists.add(appList);
        saveAppLists();
    }
    
    /**
     * 更新应用清单
     */
    public void updateAppList(int position, AppList appList) {
        if (position >= 0 && position < appLists.size()) {
            appLists.set(position, appList);
            saveAppLists();
        }
    }
    
    /**
     * 删除应用清单
     */
    public void removeAppList(int position) {
        if (position >= 0 && position < appLists.size()) {
            appLists.remove(position);
            saveAppLists();
        }
    }
    
    /**
     * 从SharedPreferences加载应用清单
     */
    private List<AppList> loadAppLists() {
        List<AppList> lists = new ArrayList<>();
        SharedPreferences prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        String listsJson = prefs.getString(KEY_LISTS, "");
        
        if (listsJson.isEmpty()) {
            return lists;
        }
        
        try {
            JSONArray jsonArray = new JSONArray(listsJson);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject listObj = jsonArray.getJSONObject(i);
                String name = listObj.getString("name");
                AppList appList = new AppList(name);
                
                JSONArray appsArray = listObj.getJSONArray("apps");
                for (int j = 0; j < appsArray.length(); j++) {
                    JSONObject appObj = appsArray.getJSONObject(j);
                    String appName = appObj.getString("appName");
                    String packageName = appObj.getString("packageName");
                    appList.addApp(new AppItem(appName, packageName));
                }
                
                lists.add(appList);
            }
        } catch (JSONException e) {
            Log.e(TAG, "Error parsing app lists", e);
        }
        
        return lists;
    }
    
    /**
     * 保存应用清单到SharedPreferences
     */
    private void saveAppLists() {
        try {
            JSONArray jsonArray = new JSONArray();
            for (AppList list : appLists) {
                JSONObject listObj = new JSONObject();
                listObj.put("name", list.getName());
                
                JSONArray appsArray = new JSONArray();
                for (AppItem app : list.getApps()) {
                    JSONObject appObj = new JSONObject();
                    appObj.put("appName", app.getAppName());
                    appObj.put("packageName", app.getPackageName());
                    appsArray.put(appObj);
                }
                
                listObj.put("apps", appsArray);
                jsonArray.put(listObj);
            }
            
            SharedPreferences prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = prefs.edit();
            editor.putString(KEY_LISTS, jsonArray.toString());
            editor.apply();
        } catch (JSONException e) {
            Log.e(TAG, "Error saving app lists", e);
        }
    }
}